//
//  RegistViewController.m
//  ios-test
//
//  Created by Apple -11 on 15/10/10.
//  Copyright (c) 2015年 Apple -11. All rights reserved.
//

#import "RegistViewController.h"
#import "Tools.h"
#import "Reachability.h"
#import "NetWork.h"
//#import "ViewController.h"
@interface RegistViewController ()

@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    userText.delegate=self;
    passText.delegate=self;
    surePassText.delegate=self;
    
    okBtn.enabled=NO;
    [okBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

    user=[NSUserDefaults standardUserDefaults];

    if (user == NULL) {
        user=[NSUserDefaults standardUserDefaults];
    }
    
    [self.view sizeToFit];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//判断几个文本框全有值时，确定按钮可按
-(void)textFieldDidEndEditing:(UITextField *)textField{
    if ([userText.text isEqualToString:@""] || [passText.text isEqualToString:@""] || [surePassText.text isEqualToString:@""]) {
        okBtn.enabled=NO;
        [okBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField.tag==100) {
        if (![passText.text isEqualToString:@""] && ![surePassText.text isEqualToString:@""]) {
            okBtn.enabled=YES;
            [okBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }else if (textField.tag==101){
        if (![userText.text isEqualToString:@""] && ![surePassText.text isEqualToString:@""]) {
             okBtn.enabled=YES;
            [okBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    
    
    return YES;
}


//键盘消失
-(IBAction)tapBackground:(id)sender{
    [userText resignFirstResponder];
    [passText resignFirstResponder];
    [surePassText resignFirstResponder];
}


//判断文本框的格式是否正确
-(IBAction)tapOK:(id)sender{
    if ([NetWork checkNetworkState]) {
        
    
    if(![Tools CheckInput1:userText.text] && ![Tools CheckInput2:userText.text]){
        
        UIAlertView *alter=nil;
        alter=[[UIAlertView alloc]initWithTitle:@"警告！！！！" message:@"账号应为手机号或者邮箱" delegate:nil cancelButtonTitle:@"close" otherButtonTitles:nil, nil];
        [alter show];
        
    }

    else if ([Tools CheckInput3:passText.text]){
        UIAlertView *alter=nil;
        alter=[[UIAlertView alloc]initWithTitle:@"警告！！！！" message:@"密码不能全是数字" delegate:nil cancelButtonTitle:@"close" otherButtonTitles:nil, nil];
        [alter show];

    }
    else if (passText.text.length<6 || passText.text.length>12) {
        UIAlertView *alter=nil;
        alter=[[UIAlertView alloc]initWithTitle:@"警告！！！！" message:@"密码是6～12位" delegate:nil cancelButtonTitle:@"close" otherButtonTitles:nil, nil];
        [alter show];
    }else if (![passText.text isEqualToString:surePassText.text]){
        UIAlertView *alter=nil;
        alter=[[UIAlertView alloc]initWithTitle:@"警告！！！！" message:@"2次密码不相同" delegate:nil cancelButtonTitle:@"close" otherButtonTitles:nil, nil];
        [alter show];
    }else if(![Tools CheckInput1:userText.text] && ![Tools CheckInput2:userText.text]){
        
        UIAlertView *alter=nil;
        alter=[[UIAlertView alloc]initWithTitle:@"警告！！！！" message:@"账号应为手机号或者邮箱" delegate:nil cancelButtonTitle:@"close" otherButtonTitles:nil, nil];
        [alter show];
    }else if([user objectForKey:userText.text]){
        UIAlertView *alter=nil;
        alter=[[UIAlertView alloc]initWithTitle:@"警告！！！！" message:@"账号已存在" delegate:nil cancelButtonTitle:@"close" otherButtonTitles:nil, nil];
        [alter show];
    }
    else{
        //if ([Tools CheckInput1:userText.text] || [Tools CheckInput2:userText.text]) {
        

            [self.view endEditing:YES];
        
            NSString *username=userText.text;
            NSString *password=passText.text;
            
            [user setObject:password forKey:username];
//            [user setObject:username forKey:@"username"];
//            [user setObject:password forKey:@"password"];
        
            
            [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(Show) userInfo:nil repeats:NO];
            
            test=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
            test1=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
            test2=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            test3=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
            
            
            CGSize size=[[UIApplication sharedApplication]keyWindow].frame.size;
            test.center=self.view.center;
            test1.frame=CGRectMake(size.height/2.5, size.height/4.5, 50, 50);
            test2.frame=CGRectMake(size.height/2.5, size.height/3.2, 50, 50);
            test3.frame=CGRectMake(size.height/2.5, size.height/2.5, 50, 50);
            [self.view addSubview:test];
            [self.view addSubview:test1];
            [self.view addSubview:test2];
            [self.view addSubview:test3];
            test.color=[UIColor grayColor];
            test1.color=[UIColor grayColor];
            test2.color=[UIColor grayColor];
            test3.color=[UIColor grayColor];
            [test startAnimating];
            [test1 startAnimating];
            [test2 startAnimating];
            [test3 startAnimating];
            
            //[test setHidesWhenStopped:YES];
        }
    }else{
        UIAlertView *alter=nil;
        alter=[[UIAlertView alloc]initWithTitle:@"警告！！！！" message:@"没有网" delegate:nil cancelButtonTitle:@"close" otherButtonTitles:nil, nil];
        [alter show];
    }
}
//注册正确后弹出框
-(void)Show{
    [[MLoginService sharedClient]getProductListWithUsername:userText.text password:passText.text completion:^(NSDictionary *result) {
        
        NSLog(@"-----账号：%@",result[@"username"]);
        NSLog(@"-----密码：%@",result[@"password"]);
        
        UIAlertView *alter=nil;
        alter=[[UIAlertView alloc]initWithTitle:@"提示" message:@"注册成功" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"跳转到登录界面", nil];
        [alter show];
        
        [test stopAnimating];
        [test1 stopAnimating];
        [test2 stopAnimating];
        [test3 stopAnimating];
        
    } failure:^(NSString *error) {
        UIAlertView *alter=nil;
        alter=[[UIAlertView alloc]initWithTitle:@"提示" message:@"错误" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alter show];
    }];
   
}

//弹出框点击返回
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==1) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else if (buttonIndex==0){
        userText.text=NULL;
        passText.text=NULL;
        surePassText.text=NULL;
    }
}

//返回按钮
-(IBAction)tapBack:(id)sender{
//    ViewController *view=[[ViewController alloc]init];
//    view.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
//    [self presentViewController:view animated:YES completion:nil];
    [self dismissViewControllerAnimated:YES completion:nil];
}

////判断手机号
//-(BOOL)CheckInput1:(NSString *)text{
//
//    NSString *Regex=@"^1[34578]\\d{9}$";
//    NSPredicate *emailTest=[NSPredicate predicateWithFormat:@"SELF MATCHES%@",Regex];
//    return [emailTest evaluateWithObject:text];
//}
////判断邮箱
//-(BOOL)CheckInput2:(NSString *)text{
//    NSString *Regex=@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
//    NSPredicate *emailTest=[NSPredicate predicateWithFormat:@"SELF MATCHES%@",Regex];
//    return [emailTest evaluateWithObject:text];
//}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
